<template>
  <div class="catemanager">
    <myheader title="频道管理">
      <span
        slot="left"
        class="iconfont iconjiantou2"
        @click="$router.back()"
      ></span>
    </myheader>
    <div class="mychannel">
      <h3>点击删除频道</h3>
      <div class="list">
        <span v-for="(v, i) in catelist" :key="i" @click="remove(v, i)">{{
          v.name
        }}</span>
      </div>
    </div>
    <div class="youchannel">
      <h3>点击添加频道</h3>
      <div class="list">
        <span v-for="(v, i) in youcatelist" :key="i" @click="add(v, i)">{{
          v.name
        }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import myheader from "@/components/myheader.vue";
import { getCateList } from "@/apis/category.js";
export default {
  data() {
    return {
      catelist: [],
      catelist: [], // 已添加的栏目数据  heimatoutiao_exsists_cate_56
      youcatelist: [], // 未添加的栏目数据  heimatoutiao_nothas_cate_56
    };
  },
  components: {
    myheader,
  },
  methods: {
    remove(v, i) {
      // 将当前栏目添加到已删除的栏目数据中
      this.youcatelist.push(v);
      // 将当前栏目从已添加栏目中移除
      this.catelist.splice(i, 1);
      // 实现本地存储
      localStorage.setItem(
        "heimatoutiao_exsists_cate_56",
        JSON.stringify(this.catelist)
      );
      localStorage.setItem(
        "heimatoutiao_nothas_cate_56",
        JSON.stringify(this.youcatelist)
      );
    },
    add(v, i) {
      this.catelist.push(v);
      this.youcatelist.splice(i, 1);
      // 实现本地存储
      localStorage.setItem(
        "heimatoutiao_exsists_cate_56",
        JSON.stringify(this.catelist)
      );
      localStorage.setItem(
        "heimatoutiao_nothas_cate_56",
        JSON.stringify(this.youcatelist)
      );
    },
  },
  async mounted() {
    // 获取所有栏目数据
    this.catelist = (await getCateList()).data.data;
    // 判断是否登陆，如果登陆就删除掉“关注”和“头条”，否则就删除“头条”
    if (localStorage.getItem("token_info")) {
      this.catelist = this.catelist.splice(2);
      // 获取所有栏目数据,我得先判断是否有本地存储，如果没有则获取服务器数据，如果有，则直接读取本地存储
      if (localStorage.getItem("heimatoutiao_exsists_cate_56")) {
        this.catelist = JSON.parse(
          localStorage.getItem("heimatoutiao_exsists_cate_56")
        );
      } else {
        this.catelist = this.catelist.splice(1);
        this.catelist = (await getCateList()).data.data;
        // 判断是否登陆，如果登陆就删除掉“关注”和“头条”，否则就删除“头条”
        if (localStorage.getItem("token_info")) {
          this.catelist = this.catelist.splice(2);
        } else {
          this.catelist = this.catelist.splice(1);
        }
      }
      if (localStorage.getItem("heimatoutiao_nothas_cate_56")) {
        this.youcatelist = JSON.parse(
          localStorage.getItem("heimatoutiao_nothas_cate_56")
        );
      }
      console.log(this.catelist);
    }
  },
};
</script>

<style lang="less" scoped>
.mychannel,
.youchannel {
  padding: 10px;
  h3 {
    line-height: 30px;
    font-weight: bold;
  }
  .list {
    padding: 10px;
    display: flex;
    flex-wrap: wrap;
    > span {
      width: 20%;
      margin-left: 10px;
      margin-top: 10px;
      border: 1px solid #ccc;
      text-align: center;
      line-height: 40px;
    }
  }
}
</style>